﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Talk.Common;

namespace TalkServer
{
    public partial class UserData : List<USER>
    {
        const string _className = "UserData";

        public UserData()
        #region >><<
        {
            initIOData();
        }

        public bool isDispose { get; protected set; }
        public void Dispose()
        {
            isDispose = true;
            this.Clear();
        }
        #endregion

        /// <summary>
        /// 從服務器獲得全部的登入端
        /// </summary>
        /// <param name="lst"></param>
        public void ServerUser(List<MSGGolbalInfo.Account> lst, out  List<GROUP> lst2)
        {
            this.Clear();
            lst2 = new List<GROUP>();

            if (lst == null || lst.Count == 0) return;

            for (int ii = 0; ii < lst.Count; ii++)
            //lst.ForEach(r =>
            #region >><<
            {
                MSGGolbalInfo.Account r = lst[ii];
                USER item1;
                List<int> lst3;
                if (Load(r.UserName, out item1, out lst3))
                {
                    item1.isOnline = true;
                    if (lst3 != null)
                    {
                        for (int ij = 0; ij < lst3.Count; ij++)
                        {
                            GROUP item2;
                            GroupData.Instance.Load(lst3[ij], out item2);
                            if (item2 != null)
                                item1.Add(item2);
                            lst2.Add(item2);
                        }
                    }
                }
                else
                {
                    item1 = new USER();
                    item1.isOnline = true;
                    item1.UserName = r.UserName;
                    item1.Section = r.ClientSection;
                }
                this.Add(item1);
            }
            //});
            #endregion


        }

        public void AddUser(MSGGolbalInfo.User item)
        {
            USER item2 = this.Find(r => r.UserName == item.UserName);
            if (item2 == null)
            {
                this.Add(new USER()
                {
                    isOnline =true,
                    Section = item.Section,
                    UserName = item.UserName,
                });
            }
            else
            {
                item2.Section = item.Section;
            }
        }

        public void DelUser(MSGGolbalInfo.User item)
        {

        }
    }
}
